------------------------------------------------
--Author Info
------------------------------------------------
SWEP.Author             = "Rothon & Modified by StargateFreak"
SWEP.Contact            = ""
SWEP.Purpose            = "Sends IDC through stargates"
SWEP.Instructions       = "Right click to set, Left click to send"
SWEP.Category			= "Stargate"
------------------------------------------------

SWEP.Spawnable = true
SWEP.Adminspawnable = true
-- First person Model
SWEP.ViewModel = "models/Madman07/GDO/GDO_v.mdl"
SWEP.ViewModelFOV = 80
-- Third Person Model
SWEP.WorldModel = "models/Madman07/GDO/GDO_w.mdl"
-- Weapon Details
SWEP.Primary.Clipsize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
SWEP.Primary.Delay = 3
SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
SWEP.timeClicked = 0

SWEP.gate = nil
local Reply

local function SendCode(EntTable)
	local code = tonumber(EntTable.Owner:GetInfo("cl_weapon_gdo_iriscode"))
	if (ValidEntity(EntTable.gate)) then
		local tgate = EntTable.gate.Target
		if (ValidEntity(tgate)) then
			local iris = EntTable:FindIris(tgate)
			local gate_pos = tgate:GetPos()
			local iris_comp = EntTable:FindEnt(gate_pos, true)
			if iris_comp and iris and (SERVER) then
				local answer = iris_comp:RecieveIrisCode(code)
				if answer == 1 or iris.IsActivated == false then -- if the Iris is Open or the Correct IDC is used which automatically opens the Iris will initiate this statement OR if the Iris is already Open.
					EntTable:SetNWString("gdo_textdisplay", "CLEAR");
				elseif answer == 0 and iris.IsActivated then -- Stops feeding back WRONG if the Iris is Open and a Correct IDC is used
					EntTable:SetNWString("gdo_textdisplay", "WRONG");
					Reply = 1
				elseif answer == 2 then
				EntTable:SetNWString("gdo_textdisplay", "STAND-BY");
				Reply = 1
				end
			elseif not iris then
				EntTable:SetNWString("gdo_textdisplay", "NO IRIS");
			elseif (not iris_comp) and iris then
				EntTable:SetNWString("gdo_textdisplay", "MANUAL IRIS");
				Reply = 1
			end
		end
	end
end

function SWEP:Reload()
end

function SWEP:Initialize()
	self:SetNWString("gdo_textdisplay", " ")
end

function Clear()

end

function SWEP:Think()
	self.gate = self:FindEnt(self.Owner:GetPos(), false)
	if (ValidEntity(self.gate)) then
		if self.gate.IsOpen then
			self.target = self.gate.Target
			if (ValidEntity(self.target)) then
				local iris = self:FindIris(self.target)
				if (ValidEntity(iris)) then
					if iris.IsIris and (Reply == 1 and iris.IsActivated == false) then
						self:SetNWString("gdo_textdisplay", "CLEAR");
						Reply = 0
						timer.Simple(5, function() self:SetNWString("gdo_textdisplay", " ") end);
					end
				end
			end
		else
			Reply = 0
		end
	else
		Reply = 0
	end
end

function SWEP:PrimaryAttack()
	local pos = self.Owner:GetPos()
	self.gate = self:FindEnt(pos, false)
	if(self.gate and self.gate.IsOpen) then
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay + 1 )
		self.Owner:SetAnimation(ACT_VM_PRIMARYATTACK);
		self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
		timer.Simple(self.Primary.Delay+1, SendCode, self)
		timer.Simple(2, function() self:SetNWString("gdo_textdisplay", tostring(self.Owner:GetInfo("cl_weapon_gdo_iriscode"))) end);
		timer.Simple(self.Primary.Delay+5, function() self:SetNWString("gdo_textdisplay", " ") end);
	end
end

function SWEP:SecondaryAttack()
	umsg.Start("gdo_openpanel", self.Owner)
	umsg.End()
end

function SWEP:FindEnt(pos, find_pc) -- modified from avon's dhd function
	local nearest
	local entDist = 2000 -- max distance to ent
	local foundEnts = {}
	if find_pc then
		for _,v in pairs(ents.FindByClass("iris_comp*")) do
			table.insert(foundEnts,v)
		end
	else
		for _,v in pairs(ents.FindByClass("stargate_*")) do
			if (v.IsStargate) then table.insert(foundEnts,v) end	
		end
	end

	for _,v in pairs(foundEnts) do
		local foundEnt_dist = (pos - v:GetPos()):Length()
		if entDist >= foundEnt_dist then
			entDist = foundEnt_dist
			nearest = v
		end
	end
	return nearest
end

function SWEP:FindIris(Gate) -- modified from avon's dhd function
	local nearest
	local entDist = 10 -- max distance to ent
	if Gate then
		local gpos = Gate:GetPos()
		for _,v in pairs(ents.FindByClass("*_iris")) do
			local foundEnt_dist = (gpos - v:GetPos()):Length()
			if (v.IsIris) then
				if entDist >= foundEnt_dist then
					entDist = foundEnt_dist
					nearest = v
				end
			end
		end
	end
	return nearest
end
